package com.bjb.vr.operation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjb.vr.common.ao.ResDevAuthorityAo;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.ResDevAuthorityService;
import com.bjb.vr.common.vo.ResDevAuthorityVo;
import com.bjb.vr.operation.service.ResDeviceCtrlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version 1.0.0
 * @description:设备授权管理
 * @author: caihaibo
 * @createDate: 2022/8/4
 */
@Slf4j
@RestController
@RequestMapping("/api/operation/v1")
public class ResDevAuthorityController {

    @Autowired
    ResDevAuthorityService resDevAuthorityService;

    @Autowired
    ResDeviceCtrlService resDeviceCtrlService;
    /**
     * 设备授权管理-查询设备授权列表
     * @param resDevAuthorityAo
     * @return
     */
    @PostMapping("/queryResDevAuthorityPage")
    public BaseResult<IPage<ResDevAuthorityVo>> queryResDevAuthorityPage(@RequestBody ResDevAuthorityAo resDevAuthorityAo){
        IPage<ResDevAuthorityVo> resDevAuthorityPage = resDevAuthorityService.queryResDevAuthorityPage(resDevAuthorityAo);
        return CommonResult.Ok().setResult(resDevAuthorityPage);
    }

    /**
     * 设备授权管理-增加客户设备授权
     * @param resDevAuthorityAo
     * @return
     */
    @PostMapping("/addResDevAuthority")
    public BaseResult addResDevAuthority(@RequestBody ResDevAuthorityAo resDevAuthorityAo){
        boolean success =  resDevAuthorityService.addResDevAuthority(resDevAuthorityAo);
        if(!success){
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        resDeviceCtrlService.sendRestartCommand(resDevAuthorityAo.getTenantId(),resDevAuthorityAo.getUdids());
        return CommonResult.Ok();
    }

    /**
     * 设备授权管理-修改客户设备授权
     * @param resDevAuthorityAo
     * @return
     */
    @PostMapping("/updateResDevAuthority")
    public BaseResult updateResDevAuthority(@RequestBody ResDevAuthorityAo resDevAuthorityAo){
        boolean success =  resDevAuthorityService.updateResDevAuthority(resDevAuthorityAo);
        if(!success){
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        resDeviceCtrlService.sendRestartCommand(resDevAuthorityAo.getTenantId(),resDevAuthorityAo.getUdids());
        return CommonResult.Ok();
    }


    /**
     * 删除客户设备授权
     * @param resDevAuthorityAo
     * @return
     */
    @PostMapping("/deleteResDevAuthority")
    public BaseResult deleteResDevAuthority(@RequestBody ResDevAuthorityAo resDevAuthorityAo){
        boolean success = resDevAuthorityService.deleteResDevAuthority(resDevAuthorityAo);
        if(!success){
            return CommonResult.Fail(BaseErrorCode.ERROR_REMOVE);
        }
        return CommonResult.Ok();
    }


    /**
     * 设备授权管理-查询客户设备授权详情
     * @param resDevAuthorityAo
     * @return
     */
    @PostMapping("/queryResDevAuthorityDetail")
    public BaseResult<ResDevAuthorityVo> queryResDevAuthorityDetail(@RequestBody ResDevAuthorityAo resDevAuthorityAo){
        ResDevAuthorityVo resCusContentAuthorityVo = resDevAuthorityService.queryResDevAuthorityDetail(resDevAuthorityAo);
        return CommonResult.Ok().setResult(resCusContentAuthorityVo);
    }


}
